package com.wthink.hzgj.settings

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.RadioGroup
import android.widget.SeekBar
import android.widget.Switch
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentSettingsBinding
import com.wthink.hzgj.settings.viewmodel.SettingsViewModel
import com.wthink.hzgj.util.ScreenSettings

class SettingsFragment : Fragment() {

    private val viewModel by viewModels<SettingsViewModel>()

    private lateinit var binding: FragmentSettingsBinding

    private var inVol:Int = 0
    private var outVol:Int = 0
    private var micVol:Int = 0
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_settings,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        return binding.root
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }

        binding.settings.setOnClickListener {
            viewModel.setVol(inVol,outVol,micVol)
        }

        viewModel.getDevVol("vol")

        binding.seekbarInVol.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
                inVol = bar.progress
                //viewModel.setVol(inVol,outVol,micVol)
            }
        })

        binding.seekbarOutVol.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
                outVol = bar.progress
                //viewModel.setVol(inVol,outVol,micVol)
            }
        })

        binding.seekbarMicVol.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
                micVol = bar.progress
                //viewModel.setVol(inVol,outVol,micVol)
            }
        })

        val screenBrightness = context?.let { ScreenSettings.getScreenBrightness(it) }
        if (screenBrightness != null) {
            binding.seekbarScreen.setProgress(255-screenBrightness)
        }

        binding.seekbarScreen.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {
                ScreenSettings.saveBrightness(context!!,255-progress)
            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
            }
        })


        val isAuto = context?.let { ScreenSettings.isAutoBrightness(it) }
        if (isAuto!!) binding.autoSwitch.isChecked

        binding.autoSwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                if (isChecked) ScreenSettings.autoBrightness(context!!, false)
                else ScreenSettings.autoBrightness(context!!, false)
            }
        })
    }
}
